global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/value_added_swiss.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {



set more off
*********************************************************************************************************************************************************************
*For our analysis we need value added and employment data. We are missing that data in switzerland in the main construction, so here we construct it seperately.    *
*********************************************************************************************************************************************************************
*************************************************************************************
* 1.We need employment. 
* 1.a. First for manufacturing. This is sector C (10-33) in this dataset as well as 
* the EUklems dataset. 
*************************************************************************************


set obs 1 
gen year=. 
save ${tmp_dir}/temp_employ_manuf.dta, replace

foreach year of numlist 1995/2015 {

    *Imprt swiss employment data from STATEM (swiss employment statistic by the federal statistics office)
    *we only import extractive and manufacturing industries (D13&D14) the rest are year/column identifiers), we immediately discard the extracting ones here.
    * swiss employment data by industry, quarterly frequency | From BFS/OFS STATEM |https://www.bfs.admin.ch/asset/fr/32008417 | downloaded April 16th 2019 
    import excel ${mow_data_raw}/swiss_data/je-f-06.02.00.01.01.xlsx, sheet("Equivalents plein temps - Total") cellrange(D4:DJ13) firstrow clear

    *rename all variables to a1, a2 etc for reshaping
    local vars = "D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH BI BJ BK BL BM BN BO BP BQ BR BS BT BU BV BW BX BY BZ CA CB CC CD CE CF CG CH CI CJ CK CL CM CN CO CP CQ CR CS CT CU CV CW CX CY CZ DA DB DC DD DE DF DG DH DI"
    
    local counter = 1

    * Loop through each variable and rename it
    foreach var of local vars {
        rename `var' a`counter'
        local counter = `counter' + 1
    }



    keep if _n==9 
    gen mgo=_n
    reshape long a, i(mgo) j(a_)
    drop mgo DJ
    *the data is at quarterly frequency. we average out to yearly
    keep if a_>=4*(`year'-1991)-1 & a_<=4*(`year'-1991)+2
    destring a, replace
    collapse (mean) a
    rename a employment
    gen year=`year' 
    append using ${tmp_dir}/temp_employ_manuf.dta
    drop if year==. 
    sleep 2000
    save ${tmp_dir}/temp_employ_manuf.dta, replace

}

*add an identifier
gen code = "D"
sleep 2000
save ${tmp_dir}/temp_employ_manuf.dta, replace

********************************
*1.b same for total employment (D8)
*********************************
clear
set obs 1 
gen year=. 
save ${tmp_dir}/temp_employ_tot.dta, replace

foreach year of numlist 1995/2015 {
    * swiss employment data by industry, quarterly frequency | 
    * From BFS/OFS STATEM |https://www.bfs.admin.ch/asset/fr/32008417 | downloaded April 16th 2019 
    import excel ${mow_data_raw}/swiss_data/je-f-06.02.00.01.01.xlsx, sheet("Equivalents plein temps - Total") cellrange(D4:DJ8) firstrow clear

    *rename all variables to a1, a2 etc for reshaping
    local vars = "D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH BI BJ BK BL BM BN BO BP BQ BR BS BT BU BV BW BX BY BZ CA CB CC CD CE CF CG CH CI CJ CK CL CM CN CO CP CQ CR CS CT CU CV CW CX CY CZ DA DB DC DD DE DF DG DH DI"
    
    local counter = 1

    * Loop through each variable and rename it
    foreach var of local vars {
        rename `var' a`counter'
        local counter = `counter' + 1
    }

    keep if _n==4 
    gen mgo=_n
    reshape long a, i(mgo) j(a_)
    drop mgo DJ
    *the data is at quarterly frequency. we average out to yearly
    keep if a_>=4*(`year'-1991)-1 & a_<=4*(`year'-1991)+2
    destring a, replace
    collapse (mean) a
    rename a employment
    gen year=`year' 
    append using ${tmp_dir}/temp_employ_tot.dta
    drop if year==. 
    sleep 2000
    save ${tmp_dir}/temp_employ_tot.dta, replace

}

sleep 2000
gen code="TOT"
save ${tmp_dir}/temp_employ_tot.dta, replace



*************************************************************************************
*2.Now for value added. 
*************************************************************************************
*************************************************************************************
*2.a.First for manufacturing. Start with smaller sectors, but some are missing
*************************************************************************************

clear
set obs 1
gen year=.
save ${tmp_dir}/temp_va_manuf.dta, replace


foreach year of numlist 1997/2015 {
    * je-f-04.02.03.01.xlsx |Swiss data on production and value added by industry | BFS/OFS national accounts | 
    *https://www.bfs.admin.ch/bfs/fr/home/statistiques/economie-nationale/comptes-nationaux/production.assetdetail.27065089.html | downlaoded April 16th 2019
    import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.01.xlsx, sheet("Branches N") cellrange(A2:CE25) firstrow clear
    *drop extractive industries, agriculture and fishing, as well as a column identifier
    drop if inlist(Aprixcourantsenmillionsde,"NOGA","01 - 03","05 - 09")
    drop B
    drop if _n==1
    destring a*, replace
    collapse (sum) a*
    gen mgo=1
    reshape long a, i(mgo) j(a_)
    if `year'==1997 {
        keep if a_==3
    }
    if `year'>1997 {
        keep if a_==4*(`year'-1997)+2
    }

    gen year=`year'
    keep a year
    rename a VA
    append using ${tmp_dir}/temp_va_manuf.dta
    drop if year==.
    sleep 2000
    save ${tmp_dir}/temp_va_manuf.dta, replace
}

******************************************************************************************************************
* We need to add data for 1995 and 1996. We just impute growth rate from a slightly bigger sector in this data set
******************************************************************************************************************

clear

set obs 1
gen year=. 
save ${tmp_dir}/temp_va_larger_sector.dta, replace

foreach year of numlist 1995/1997 {
    *Swiss data on production and value added by sector | BFS/OFS national accounts |
    * https://www.bfs.admin.ch/bfs/fr/home/statistiques/economie-nationale/comptes-nationaux/production.assetdetail.27065111.html | downloaded April 16th 2019
    if `year'==1995 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(F6:F6) clear
        rename F VA_large
    }
    if `year'==1996 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(I6:I6) clear
        rename I VA_large
    }
    if `year'==1997 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(M6:M6) clear
        rename M VA_large
    }
    gen year=`year'
    append using ${tmp_dir}/temp_va_larger_sector.dta
    drop if year==.
    sleep 2000
    save ${tmp_dir}/temp_va_larger_sector.dta, replace 

}


merge 1:1 year using ${tmp_dir}/temp_va_manuf.dta

*using the growthrates from the larger sector to impute the missing values in the manufcaturing sector
gsort -year
replace VA=VA[_n-1]*VA_large[_n]/VA_large[_n-1] if VA==.
gen code="D"
drop VA_large _merge

*complete set from 1995-2015
save ${tmp_dir}/temp_va_manuf.dta, replace



*************************************************************************************
*2.b And for total industires
*************************************************************************************

clear
set obs 1
gen year=. 
save ${tmp_dir}/temp_va_tot.dta, replace

foreach year of numlist 1995/2015 {
    *Swiss data on production and value added by sector | BFS/OFS national accounts |
    * https://www.bfs.admin.ch/bfs/fr/home/statistiques/economie-nationale/comptes-nationaux/production.assetdetail.27065111.html | downloaded April 16th 2019
    if `year'==1995 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(F8:F8) clear
        rename F VA
    }
    if `year'==1996 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(I8:I8) clear
        rename I VA
    }
    if `year'==1997 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(M8:M8) clear
        rename M VA
    }
    if `year'==1998 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(Q8:Q8) clear
        rename Q VA
    }
    if `year'==1999 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(U8:U8) clear
        rename U VA
    }
    if `year'==2000 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(Y8:Y8) clear
        rename Y VA
    }
    if `year'==2001 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(AC8:AC8) clear
        rename AC VA
    }
    if `year'==2002 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(AG8:AG8) clear
        rename AG VA
    }
    if `year'==2003 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(AK8:AK8) clear
        rename AK VA
    }
    if `year'==2004 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(AO8:AO8) clear
        rename AO VA
    }
    if `year'==2005 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(AS8:AS8) clear
        rename AS VA
    }
    if `year'==2006 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(AW8:AW8) clear
        rename AW VA
    }
    if `year'==2007 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(BA8:BA8) clear
        rename BA VA
    }
    if `year'==2008 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(BE8:BE8) clear
        rename BE VA
    }
    if `year'==2009 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(BI8:BI8) clear
        rename BI VA
    }
    if `year'==2010 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(BM8:BM8) clear
        rename BM VA
    }
    if `year'==2011 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(BQ8:BQ8) clear
        rename BQ VA
    }
    if `year'==2012 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(BU8:BU8) clear
        rename BU VA
    }
    if `year'==2013 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(BY8:BY8) clear
        rename BY VA
    }
    if `year'==2014 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(CC8:CC8) clear
        rename CC VA
    }
    if `year'==2015 {
        import excel ${mow_data_raw}/swiss_data/je-f-04.02.03.04.xlsx, sheet("Sect. éco. N") cellrange(CG8:CG8) clear
        rename CG VA
    }



    gen year=`year'
    append using ${tmp_dir}/temp_va_tot.dta
    drop if year==.
    sleep 2000
    save ${tmp_dir}/temp_va_tot.dta, replace 

}

gen code="TOT"
save ${tmp_dir}/temp_va_tot.dta, replace


*************************************************************************************
*Merge all together 
*************************************************************************************

merge 1:1 year using ${tmp_dir}/temp_employ_tot.dta
drop _merge
save ${tmp_dir}/temp_va_empl_tot.dta, replace

use  ${tmp_dir}/temp_va_manuf.dta, clear
merge 1:1 year using ${tmp_dir}/temp_employ_manuf.dta
drop _merge

append using ${tmp_dir}/temp_va_empl_tot.dta

label var VA "Value added"
label var employment "Employment"
label var year "Year"
label var code "Sector code"

save ${mow_data_proc}/swiss_VA.dta, replace

*************************************************************************************
* 3.  And we need it by hour
*************************************************************************************
* Strictly speaking we need full-time hours for manufacturing, but that doesn't exist
* We therefore use full time workers for total sectors
* Employment above is for full-time equivalent. 


clear
set obs 1
gen year=.
save ${tmp_dir}/temp_hours.dta, replace


foreach year of numlist 1995/2015 {
    *Swiss statistics on average working hours | From BFS/OFS SVOLTA | 
    *https://www.bfs.admin.ch/bfs/fr/home/statistiques/travail-remuneration/activite-professionnelle-temps-travail/heures-travail/heures-effectives-travail.assetdetail.31025792.html | downloaded April 16th 2019
    *only importing the fulltime quotas
    import excel ${mow_data_raw}/swiss_data/je-f-03.02.03.01.02.02.xlsx, sheet("`year'") cellrange(L10:L10) clear
    rename L hours
    gen year=`year'
    append using ${tmp_dir}/temp_hours.dta
    drop if year==.
    sleep 1500
    save ${tmp_dir}/temp_hours.dta, replace

}

*We use those hours for both manufacturing and total industries. 
expand 2
sort hours year
quietly by hours year:  gen dup = cond(_N==1,0,_n)
gen code="TOT"
replace code="D" if dup==2
drop dup

*merge them together with the full set
merge 1:1 year code using ${mow_data_proc}/swiss_VA.dta, nogen
gen va_emp=VA/(employment*hours)*1000000
drop hours employment

label var va_emp "Value added per hour worked"
label var code "sector code"

save ${mow_data_proc}/swiss_va_emp.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat